package com.hdoit.ares.logical.insu.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.insu.dao.IInsuranceDao;
import com.hdoit.ares.logical.insu.mapper.InsuranceMapper;
import com.hdoit.ares.logical.insu.model.Insurance;
import com.hdoit.ares.logical.insu.model.InsuranceCompany;
import com.hdoit.ares.logical.insu.model.InsuranceInsurancePeople;
import com.hdoit.ares.logical.insu.model.InsurancePeople;
import com.hdoit.ares.logical.viewmodel.insu.FindInsu;
import com.hdoit.ares.logical.viewmodel.insu.InsuranceSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      lvsi
 * @email       
 * @createtime  2017年12月09日 19:10:15
 * @comment
 */
@Component
public class InsuranceDaoImpl extends SuperDao<Insurance> implements IInsuranceDao{

	private InsuranceMapper insuranceMapper;
	public InsuranceMapper getInsuranceMapper() {
		return insuranceMapper;
	}

	@Resource
	public void setInsuranceMapper(InsuranceMapper insuranceMapper) {
		this.insuranceMapper = insuranceMapper;
	}

	@Override
	public List<Insurance> queryInsuranceList(InsuranceSearch insuranceSearch,PagerModel pagerModel) {
		
		return insuranceMapper.queryInsuranceList(insuranceSearch, pagerModel);
	}

	@Override
	public int queryInsuranceCount(InsuranceSearch insuranceSearch) {
		
		return insuranceMapper.queryInsuranceCount(insuranceSearch);
	}
	@Override
	public List<Insurance> queryListByFindInsu(FindInsu findInsu) {
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("insu.*","insucom.`icon` AS logoUrl","insucom.`shortName`")
						.from(Insurance.class,"insu")
						.leftJoin(InsuranceInsurancePeople.class,"insupeo").on("insu.`id`=insupeo.`insuranceId`")
						.leftJoin(InsurancePeople.class,"peo").on("insupeo.`insurancePeopleId` = peo.`id`")
						.leftJoin(InsuranceCompany.class,"insucom").on("insucom.`id` = insu.`insuranceCompanyId`")
						.andEq("insu.`deleteMark`", "1")
						.andEq("insu.`state`","grounding")
						.addGroups("insu.`id`");
		
						if(!StringUtils.isEmpty(findInsu.getInsuCompany())){
							finderExpression.andEq("insucom.`shortName`", findInsu.getInsuCompany());
						}
						if(!StringUtils.isEmpty(findInsu.getInsuOne())){
							finderExpression.andEq("insu.`insuranceOne`", findInsu.getInsuOne());
						}
						if(!StringUtils.isEmpty(findInsu.getInsuTwo())){
							finderExpression.andEq("insu.`insuranceTwo`", findInsu.getInsuTwo());
						}
						if(!StringUtils.isEmpty(findInsu.getPeople())){
							finderExpression.andEq("peo.`name`", findInsu.getPeople());
						}
						if(!StringUtils.isEmpty(findInsu.getStatus())){
							finderExpression.andEq("insu.`status`", findInsu.getStatus());
						}
						if(!StringUtils.isEmpty(findInsu.getOrder())){
							if("desc".equals(findInsu.getUpOrDown())){
								finderExpression.addOrder("insu."+findInsu.getOrder(), OrderEnum.desc);
							}
							if("asc".equals(findInsu.getUpOrDown())){
								finderExpression.addOrder("insu."+findInsu.getOrder(), OrderEnum.asc);
							}
						}if(!StringUtils.isEmpty(findInsu.getIsAllPage())){
							finderExpression.limit(0,findInsu.getPageSize()*findInsu.getPageNum());
						}else if(findInsu.getPageNum()!=null){
							int start = (findInsu.getPageNum()-1)*findInsu.getPageSize();
							finderExpression.limit(start,findInsu.getPageSize());
						}
																							
		return queryList(finderExpression);
		
	}

	@Override
	public int updateUndercarriage(InsuranceSearch insuranceSearch) {
		
		return insuranceMapper.updateUndercarriage(insuranceSearch);
	}

	@Override
	public int updateShelf(InsuranceSearch insuranceSearch) {
		
		return insuranceMapper.updateShelf(insuranceSearch);
	}
}
